﻿// -----------------------------------------------------------------------
// <copyright file="UserLogData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db UserLog
    /// </summary>
    public class UserLogData
    {
        /// <summary>
        /// Get UserLog By Id
        /// </summary>
        /// <param name="userLogId">Param userLogId</param>
        /// <returns>Return UserLog</returns>
        public UserLogEN GetUserLogById(int userLogId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                UserLogEN userLog = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserLogGetById, userLogId);
                while (reader.Read())
                {
                    userLog = new UserLogEN();

                    if (!reader.IsDBNull(0))
                    {
                        userLog.UserLogId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        userLog.UserId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        userLog.DepartmentId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        userLog.FunctionId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        userLog.Even = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        userLog.Date = reader.GetDateTime(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        userLog.Descriptions = reader.GetString(6);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return userLog;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all UserLog
        /// </summary>
        /// <returns>Return list all UserLog</returns>
        public IList<UserLogEN> GetUserLogs()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<UserLogEN> lstUserLog = new List<UserLogEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserLogGetAll);
                while (reader.Read())
                {
                    UserLogEN userLog = new UserLogEN();

                    if (!reader.IsDBNull(0))
                    {
                        userLog.UserLogId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        userLog.UserId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        userLog.DepartmentId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        userLog.FunctionId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        userLog.Even = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        userLog.Date = reader.GetDateTime(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        userLog.Descriptions = reader.GetString(6);
                    }

                    lstUserLog.Add(userLog);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstUserLog;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db UserLog
        /// </summary>
        /// <param name="userLog">Paarm UserLog</param>
        /// <returns>Insert UserLog</returns>
        public int Insert(UserLogEN userLog)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.UserId, userLog.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.DepartmentId, userLog.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.FunctionId, userLog.FunctionId),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.Even, userLog.Even),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.Date, (object)userLog.Date ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.Descriptions, userLog.Descriptions)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserLogInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db UserLog
        /// </summary>
        /// <param name="userLog">Param UserLog</param>
        public void Update(UserLogEN userLog)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.UserLogId, userLog.UserLogId),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.UserId, userLog.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.DepartmentId, userLog.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.FunctionId, userLog.FunctionId),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.Even, userLog.Even),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.Date, (object)userLog.Date ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.UserLog.Descriptions, userLog.Descriptions)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserLogUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete UserLog by userLogId
        /// </summary>
        /// <param name="userLogId">param userLogId</param>
        public void Delete(int userLogId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.UserLog.UserLogId, userLogId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserLogDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}